# -*- coding: utf-8 -*-

# Created on 06/08/2010

# Copyright (C) 2010  Sergio Rodrigo

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


ACTOR_ACTION = 0
INTERRUPTION_ACTION = 1

class Control():
    """ It behaves as a driver: reads the component's value and do the action.
    
    * Methods:
    __init__(self, name="", peripheral=None, controllableActor=None)
    initialize(self)
    update(self)
    _makeActions(self) -- Must be overriden.
    readInterruption(self)
    readAction(self)
    
    * Member variables:
    name -- String name of the control
    peripheral -- Peripheral object linked to the control
    controllableActor -- ControllableActor object linked to the control
    actions -- A list of Action objects
    interruptions -- A list of Action objects
    
    Must be subclassed.
    
    """

    def __init__(self, name, peripheral, controllableActor):
        """ Constructor.
        
        * Arguments:
        name -- String name of the control (default "")
        peripheral -- Peripheral object linked to the control (default None)
        controllableActor -- ControllableActor object linked to the control \
            (default None)
        
        """
        self.name = name
        self.peripheral = peripheral
        self.controllableActor = controllableActor
        self.actions = []
        self.interruptions = []
        #self.MAP_DATA = {
        #           # Component.value : ACTION_TYPE (ACTOR_ACTION or INTERRUPTION_ACTION)
        #           }

    def initialize(self):
        """ Initializes the control and its peripheral.
        
        """
        self.peripheral.initialize()

    def update(self):
        """ Updates the control. Makes actions with data in the peripheral.
        
        """
        self.peripheral.update()
        self._makeActions()

    def _makeActions(self):
        """ Reads data in the peripheral and makes the actions associated.
        
        Must be overriden.
        
        """
        raise NotImplementedError

    def readInterruption(self):
        """ Reads and pops an interruption in interruptions list.
        
        return -- interruption if it exists
                  None otherwise.
        
        """
        return self.interruptions.pop(0) if self.interruptions else None

    def readAction(self):
        """ Reads and pops an interruption in the actions list.
        
        return -- action if it exists.
                  None otherwise.
        
        """
        return self.actions.pop(0) if self.actions else None

# eof
